<template>
  <div style="width: 100%">
    <one v-if="mIdx==0" ref="one" />
    <two v-else-if="mIdx==1" ref="two" :data="info" />
    <three v-else-if="mIdx==2" ref="three" :data="info" />
    <a-card class="card" :bordered="false" style="margin-top: 20px;">
      <div style="text-align: right;padding-top: 20px;">
        <template v-if="mIdx==0">
          <a-button type="primary" @click="handleSave">保存分组并进行下一步操作</a-button>
        </template>
        <template v-else-if="mIdx==2">
          <a-button type="primary" @click="handleSaveMember">提交</a-button>
          <a-button type="primary" @click="handleReset">返回上一步</a-button>
        </template>
      </div>
    </a-card>
  </div>
</template>
<script>
import moment from 'moment'

import one from './groupInfo/one.vue'
import two from './groupInfo/two.vue'
import three from './groupInfo/three.vue'

const selectArr = ['TYPE0126', 'TYPE0075','TYPE0372','TYPE0065','TYPE0342','TYPE0370']
export default {
  props: {
    editVisible: {
      default: false
    }
  },
  components: {
    one,
    two,
    three
  },
  data() {
    return {
      mIdx: 0,
      info: {}
    }
  },
  created() {
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    // 关闭
    handleCancel() {
      this.$emit('close')
    },
    // 保存分组并进行下一步操作
    handleSave() {
      let isGenerateNew = this.$refs.one.generateNew
      if (isGenerateNew === '') {
        return this.$message.warn('请先选择创建分组的方式')
      }
      this.$refs.one.form.validateFields(
        (err, values) => {
          if (!err) {
            this.$DateToString(values)
            if (isGenerateNew === 0) {
              this.$api.degree.pleaGroupCheck(values).then(res => {
                this.$DateToString(res.data)
                this.info = Object.assign({},res.data,this.$refs.one.selectNames);
                this.$nextTick(()=>{
                  this.mIdx = 2;
                })
                // let params = Object.assign({}, r.data, values)
                // delete params.dbfzbh
                // delete params.id
                // this.$api.degree.pleaGroupSave(params).then(res => {
                //   this.$DateToString(res.data)
                //   this.$success({
                //     title: '生成成功',
                //     content: '生成的分组号为' + res.data.dbfzbh,
                //   });
                //   this.info = Object.assign({},res.data,this.$refs.one.selectNames);
                //   this.$nextTick(()=>{
                //     this.mIdx = 2;
                //   })
                // })
              })
            } else if (isGenerateNew === 1) {
              let params = Object.assign({},values)
              this.$api.degree.pleaGroupSave(params).then(res => {
                this.$DateToString(res.data)
                this.$success({
                  title: '生成成功',
                  content: '生成的分组号为' + res.data.dbfzbh,
                });
                this.info = Object.assign({},res.data,this.$refs.one.selectNames);
                this.$nextTick(()=>{
                  this.mIdx = 2;
                })
              })
            }
            // let params = Object.assign({},values,{
            //   dbrq :values.dbrq.format('YYYY-MM-DD')
            // })
            // this.$api.degree.pleaGroupSave(params).then(res => {
            //   if (res.data.dbrq instanceof moment) {
            //     res.data.dbrq = values.dbrq.format('YYYY-MM-DD')
            //   }
            //   this.info = Object.assign({},res.data,this.$refs.one.selectNames);
            //   this.$nextTick(()=>{
            //     this.mIdx = 1;
            //   })
            // })
          }
        },
      );
    },
    //保存第二部修改
    handleSaveStudent(){
      let studentList = this.$refs.two.checkedStudent;
      this.$api.degree.pleaGroupSetStudent(studentList).then(res=>{
        this.$message.success('保存成功');
        this.mIdx=2;
      })
    },
    //保存第三步
    handleSaveMember(){
      let param = this.$refs.three.memberData;
      if (param.length%2==0) {
        this.$message.warn("委员数量必须为奇数");
        return;
      }
      this.$api.degree.pleaGroupSetMember(param).then(res=>{
        this.$message.success('提交成功');
      })
    },
    //重新生成
    handleReset(){
      this.mIdx = 0;
      this.info = {};
    }
  }
}

</script>
<style lang="less" scoped>
.card {
  margin-bottom: 20px;
}

</style>
